using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace PConsulting.DataAccess
{
    class SqlHelper
    {
        public static DataSet GetDataSet(string connectionString, string storedProcedureName, params SqlParameter[] parameters)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand();
                command.CommandText = storedProcedureName;
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = conn;
                command.Parameters.AddRange(parameters);

                SqlDataAdapter da = new SqlDataAdapter(command);
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error while retrieving data", ex);
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
        }

        public static void ExecuteSql(string connectionString, string storedProcedureName, params SqlParameter[] parameters)
        {
            SqlConnection conn = new SqlConnection(connectionString);

            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand();
                command.CommandText = storedProcedureName;
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = conn;
                command.Parameters.AddRange(parameters);

                command.ExecuteNonQuery();         
                
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error while retrieving data", ex);
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
        }

        public static object GetScalar(string connectionString, string storedProcedureName, params SqlParameter[] parameters)
        {
            SqlConnection conn = new SqlConnection(connectionString);

            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand();
                command.CommandText = storedProcedureName;
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = conn;
                command.Parameters.AddRange(parameters);

                return command.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error while retrieving data", ex);
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
        }
    }
}
